<template>
  <div class="login-body">
    <div class="login-container">
      <div class="head">
        <img class="logo" src="../assets/logo.png" />
        <div class="name">
          <div class="title">PMall积分商城</div>
          <div class="tips">后台管理系统</div>
        </div>
      </div>
      <el-form ref="loginForm" :rules="state.rules" :model="state.ruleForm" class="login-form">
        <el-form-item label="账号" prop="username">
          <el-input v-model.trim="state.ruleForm.username" type="text" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model.trim="state.ruleForm.password" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item>
          <div style="color: #333">登录表示您已同意<a>《服务条款》</a></div>
          <el-button style="width: 100%" :loading="state.loading" type="primary" @click="submitForm">
            立即登录
          </el-button>
          <el-checkbox v-model="state.checked" @change="!state.checked"> 下次自动登录 </el-checkbox>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import api from '@/utils/api'
import md5 from 'js-md5'
import { onMounted, reactive, ref } from 'vue'
import { localSet } from '@/utils'
import { useRouter } from 'vue-router'
const router = useRouter()
const loginForm = ref(null)
const state = reactive({
  ruleForm: {
    username: '',
    password: ''
  },
  checked: true,
  loading: false,
  rules: {
    username: [{ required: 'true', message: '账户不能为空', trigger: 'blur' }],
    password: [{ required: 'true', message: '密码不能为空', trigger: 'blur' }]
  }
})
onMounted(() => {
  document.title = `系统登录 | PMall积分商城`
})
const submitForm = async() => {
  loginForm.value.validate(valid => {
    if (valid) {
      state.loading = true
      api.http
        .post(api.ajaxUrl + '/users/login', {
          loginName: state.ruleForm.username || '',
          passwd: state.ruleForm.password
        })
        .then(res => {
          localSet('token', res.token)
          localSet('user', res.user)
          state.loading = false
          router.push({
            path: res.user.homePage
          })
        })
        .catch(() => {
          state.loading = false
        })
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
const resetForm = () => {
  loginForm.value.resetFields()
}
</script>

<style scoped>
.login-body {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  background-color: #fff;
}
.login-container {
  margin: 150px 0 0 0;
  width: 420px;
  height: 500px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0px 21px 41px 0px rgba(0, 0, 0, 0.2);
}
.head {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px 0 20px 0;
}
.head img {
  width: 100px;
  height: 100px;
  margin-right: 20px;
}
.head .title {
  font-size: 28px;
  color: #4ba7f9;
  font-weight: bold;
}
.head .tips {
  font-size: 12px;
  color: #999;
}
.login-form {
  width: 70%;
  margin: 0 auto;
}
.login-form >>> .el-form--label-top .el-form-item__label {
  padding: 0;
}
</style>
